/**
 * 
 */
package engine;


import java.util.ArrayList;

/**
 * This is a stub for the Source used for the sperimentation. A {@link ExternalSource}  in order to obtain pseudo-randomatic values.
 * @author  Elisa Costante
 */
public class ExternalSource implements Cloneable {

	private String identifierName;
	private ArrayList<Service> measuredServices;

	/**
	 * @param frameOfDiscernment
	 * @param name
	 */
	public ExternalSource(String name) {
		this.identifierName = name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see interfaces.IReputationSource#readMeasure(interfaces.Service,
	 * massDistribution.MeasuredAttribute)
	 */

	/**
	 * @return  the identifierName
	 * @uml.property  name="identifierName"
	 */
	public String getIdentifierName() {
		return this.identifierName;
	}

	/**
	 * @param identifierName  the identifierName to set
	 * @uml.property  name="identifierName"
	 */
	public void setIdentifierName(String identifierName) {
		this.identifierName = identifierName;
	}

	/**
	 * @return  the measuredServices
	 * @uml.property  name="measuredServices"
	 */
	public ArrayList<Service> getMeasuredServices() {
		return this.measuredServices;
	}

	/**
	 * @param measuredServices  the measuredServices to set
	 * @uml.property  name="measuredServices"
	 */
	public void setMeasuredServices(ArrayList<Service> measuredServices) {
		this.measuredServices = measuredServices;
	}

	/**
	 * @param serviceToAdd
	 */
	public void addMeasuredService(Service serviceToAdd) {
		if (measuredServices == null)
			measuredServices = new ArrayList<Service>();

		measuredServices.add(serviceToAdd);

	}

	/**
	 * Returns the service with the serviceIdentifier id or null in such service
	 * does not exist into the list of measuredServices.
	 * 
	 * @param serviceIdentifier
	 * @return
	 */
	public Service getMeasuredService(String serviceIdentifier) {
		if (measuredServices != null) {
			for (Service service : measuredServices) {
				if (((Service) service).getIdentifierName().equalsIgnoreCase(
						serviceIdentifier))
					return (Service) service;
			}
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime
				* result
				+ ((this.identifierName == null) ? 0 : this.identifierName
						.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		ExternalSource other = (ExternalSource) obj;
		if (this.identifierName == null) {
			if (other.identifierName != null) {
				return false;
			}
		} else if (!this.identifierName.equals(other.identifierName)) {
			return false;
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "ExternalSource [identifierName=" + this.identifierName
				+ ", measuredServices=" + this.measuredServices + "]";
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		ExternalSource clonedSource = new ExternalSource(identifierName);
		if (this.measuredServices != null) {
			for (Service measuredService : measuredServices) {
				Service clonedService = (Service) measuredService.clone();
				clonedSource.addMeasuredService(clonedService);
			}
		}
		return clonedSource;
	}
}
